clear all
set matsize 11000, permanently
set maxvar 120000
set seed 1
global out "C:/Userdata/Shared/Output/GOTV"

* Variable macros
global fixed_absorb birthyear 
global fixed_transform edufield UtlSvBakG incdecile
global othervars voted09 voted09_mor voted09_far voted10 voted10_mor voted10_far voted18 voted18_mor voted18_far female district_turnout familysize yearseducation missing_*

* Log file
capture log close
log using "C:/Userdata/Shared/Logs/GOTV/log.txt", replace

* Estout defaults
global statatab label ar2 se nogaps b(3) nomtitles
global latextab replace label se nogaps b(3) compress booktabs nonotes nomtitles alignment(D{.}{.}{2.3}) varlabels(_cons "Constant") star(* 0.1 ** 0.05 *** 0.01)
global mainkeep keep(treat t_f t_w t_f t_f_f t_f_s t_w t_w_f t_w_s t_f_n t_w_l)
global stats stats(N, fmt(%11.0fc) layout("\mc{@}") label("Observations"))

* Open dataset
use "E:\ProjData\GOTV\Dataset.dta", replace



********************************************************************************
// Data management suitable for this do-file
********************************************************************************

* Create dummy variables, drop one category per variable and store in macro (for sims)
global dummies
foreach v in $fixed_transform {
	tab `v', gen(`v'_d)
	drop `v'_d`r(r)'
	global dummies = "$dummies" + " " + "`v'_d*"
}

* Rescale outcome
replace voted19 = voted19 * 100

* Old approach
* Calculate vote propensity (main reg without treat)
qui reghdfe voted19 $othervars, absorb($fixed_absorb $fixed_transform) cluster(family)
predict votepropensity

* Count family members and colleagues conditioned on vote propensity
gen treat_vp1 = treat * cond(inrange(votepropensity,-50,40),1,0)
gen treat_vp2 = treat * cond(inrange(votepropensity,40,60),1,0)
gen treat_vp3 = treat * cond(inrange(votepropensity,60,150),1,0)
gen samplingframe_vp1 = samplingframe * cond(inrange(votepropensity,-50,40),1,0)
gen samplingframe_vp2 = samplingframe * cond(inrange(votepropensity,40,60),1,0)
gen samplingframe_vp3 = samplingframe * cond(inrange(votepropensity,60,150),1,0)

* Same but below or above 50
gen treat_below = treat * cond(inrange(votepropensity,-50,50),1,0)
gen treat_above = treat * cond(inrange(votepropensity,50,150),1,0)
gen samplingframe_below = samplingframe * cond(inrange(votepropensity,-50,50),1,0)
gen samplingframe_above = samplingframe * cond(inrange(votepropensity,50,150),1,0)

foreach unit in f w {
forvalues i = 1/3 {
	* Sum and then remove oneself, set treat sum to zero if missing
	gegen t_`unit'_vp`i' = total(treat_vp`i') if `unit' != ., by(`unit')
	replace t_`unit'_vp`i' = t_`unit'_vp`i' - treat_vp`i' if treat_vp`i' != .
	replace t_`unit'_vp`i' = 0 if t_`unit'_vp`i' == .
	gegen sf_`unit'_vp`i' = total(samplingframe_vp`i') if `unit' != ., by(`unit')
	replace sf_`unit'_vp`i' = sf_`unit'_vp`i' - samplingframe_vp`i' if samplingframe_vp`i' != .
	replace sf_`unit'_vp`i' = 999 if sf_`unit'_vp`i' == .
}
}

* Same but below or above 50
foreach unit in f w {
foreach i in below above {
	* Sum and then remove oneself, set treat sum to zero if missing
	gegen t_`unit'_`i' = total(treat_`i') if `unit' != ., by(`unit')
	replace t_`unit'_`i' = t_`unit'_`i' - treat_`i' if treat_`i' != .
	replace t_`unit'_`i' = 0 if t_`unit'_`i' == .
	gegen sf_`unit'_`i' = total(samplingframe_`i') if `unit' != ., by(`unit')
	replace sf_`unit'_`i' = sf_`unit'_`i' - samplingframe_`i' if samplingframe_`i' != .
	replace sf_`unit'_`i' = 999 if sf_`unit'_`i' == .
}
}


******************************************
// Same for different treatment occassions (new)
******************************************

* Count family members and colleagues conditioned on treatment occasion
forvalues i = 1/6 {
	gen treat_batch`i' = treat * cond(Group==`i',1,0)
	gen samplingframe_batch`i' = samplingframe * cond(Group==`i',1,0)
}

foreach unit in f w {
forvalues i = 1/6 {
	* Sum and then remove oneself, set treat sum to zero if missing
	gegen t_`unit'_batch`i' = total(treat_batch`i') if `unit' != ., by(`unit')
	replace t_`unit'_batch`i' = t_`unit'_batch`i' - treat_batch`i' if treat_batch`i' != .
	replace t_`unit'_batch`i' = 0 if t_`unit'_batch`i' == .
	gegen sf_`unit'_batch`i' = total(samplingframe_batch`i') if `unit' != ., by(`unit')
	replace sf_`unit'_batch`i' = sf_`unit'_batch`i' - samplingframe_batch`i' if samplingframe_batch`i' != .
	replace sf_`unit'_batch`i' = 999 if sf_`unit'_batch`i' == .
}
}


* Labels
label var birthyear "Birthyear"
label var incdecile "Income decile"
label var voted09 "Voted 2009"
label var voted09_mor "Mother voted 2009"
label var voted09_far "Father voted 2009"
label var voted10 "Voted 2010"
label var voted10_mor "Mother voted 2010"
label var voted10_far "Father voted 2010"
label var voted18 "Voted 2018"
label var voted18_mor "Mother voted 2018"
label var voted18_far "Father voted 2018"
label var female "Female"
label var district_turnout "District turnout"
label var familysize "Family size"
label var yearseducation "Years of education"


********************************************************************************
// Coefficient plot
********************************************************************************


* With controls (+ store p-values + store samples without singletons)
gen h = ""
gen b = .
gen se = .
qui reghdfe voted19 treat $othervars, absorb($fixed_absorb $fixed_transform) cluster(family)
	replace h = "Direct" if _n == 2
	replace b = _b[treat] if _n == 2
	replace se = _se[treat] if _n == 2
qui reghdfe voted19 t_f t_w  $othervars, absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace) 
	replace h = "Family" if _n == 3
	replace b = _b[t_f] if _n == 3
	replace se = _se[t_f] if _n == 3
	replace h = "Colleagues" if _n == 4
	replace b = _b[t_w] if _n == 4
	replace se = _se[t_w] if _n == 4

gen r = 6 - _n if _n < 6
gen ll = b - 1.96 * se
gen ul = b + 1.96 * se
tw (scatter r b) (rspike ll ul r, horizontal), xline(0) scheme(lean1) ytitle(" ") legend(off) ///
ylabel(4 "Direct" 3 "Family" 2 "Colleagues") ysize(1) xsize(2) scale(1.3) graphregion(margin(0 1 1 1)) ysc(outergap(-7))
graph export "$out/img/Coefficient plot.pdf", as(pdf) replace
drop h b se r ll ul

* Other versions of spillover effect
gen t_f_binary = min(t_f,1)
gen t_w_binary = min(t_w,1)
label var t_f_binary "At least one family member"
label var t_w_binary "At least one colleague"
eststo: qui reghdfe voted19 t_f t_w  $othervars, absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace) 
	estadd local sample "Main"
eststo: qui reghdfe voted19 t_f_binary t_w_binary  $othervars, absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace) 
estadd local sample "Binary"
eststo: qui reghdfe voted19 t_f $othervars, absorb($fixed_absorb $fixed_transform sf_f) cluster(family workplace) 
	estadd local sample "Separate"
eststo: qui reghdfe voted19 t_w $othervars, absorb($fixed_absorb $fixed_transform sf_w) cluster(family workplace) 	
	estadd local sample "Separate"	
esttab, $statatab keep(t_f t_w t_f_binary t_w_binary)
esttab using "$out/tex/spillover_binary.tex", $latextab keep(t_f t_w t_f_binary t_w_binary) stats(N sample, fmt(%11.0fc) layout("\mc{@}" "\mc{@}") label("Observations" "Model"))
eststo clear


********************************************************************************
// Coefficient plot with 6 time points
********************************************************************************


* With controls (+ store p-values + store samples without singletons)
gen h = ""
gen b = .
gen se = .
gen batch = .
forvalues i = 1/6 {
qui reghdfe voted19 treat_batch`i' $othervars, absorb($fixed_absorb $fixed_transform) cluster(family)
	replace h = "Direct" if _n == 1+`i'
	replace b = _b[treat_batch`i'] if _n == 1+`i'
	replace se = _se[treat_batch`i'] if _n == 1+`i'
	replace batch = `i' if _n == 1+`i'
qui reghdfe voted19 t_f_batch`i' t_w_batch`i'  $othervars, absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace) 
	replace h = "Family" if _n == 8+`i'
	replace b = _b[t_f_batch`i'] if _n == 8+`i'
	replace se = _se[t_f_batch`i'] if _n == 8+`i'
	replace batch = `i' if _n == 8+`i'
	replace h = "Colleagues" if _n == 15+`i'
	replace b = _b[t_w_batch`i'] if _n == 15+`i'
	replace se = _se[t_w_batch`i'] if _n == 15+`i'
	replace batch = `i' if _n == 15+`i'
}
gen r = 22 - _n if _n < 22
gen ll = b - 1.96 * se
gen ul = b + 1.96 * se

tw (line b batch if h == "Direct") (line b batch if h == "Family") (line b batch if h == "Colleagues"), scheme(lean1) yline(0) ytitle("Estimated effect") xtitle("Batch") legend(pos(6)row(1)lab(1 "Direct")lab(2 "Household")lab(3 "Colleagues"))

tw (scatter r b) (rspike ll ul r, horizontal), xline(0) scheme(lean1) ytitle(" ") legend(off) ///
ylabel(18 "Direct" 11 "Family" 4 "Colleageus") ysize(1) xsize(2) scale(1.3) graphregion(margin(0 1 1 1)) ysc(outergap(-7))

replace r = _n-1 if b != .
replace r = r + floor(r/7)*3
tw (scatter b r) (rspike ll ul r), yline(0) scheme(lean1) ytitle(" ") legend(off) ///
ysize(1) xsize(2) scale(1.3) graphregion(margin(0 1 1 1)) ysc(outergap(-7)) ///
xla(3.5 "Direct" 13.5 "Household" 23.5 "Colleagues") ytitle("Estimated effect") xtitle(" ")
graph export "$out/img/Batches.pdf", as(pdf) replace
drop h b se r ll ul


********************************************************************************
// Regression table over vote propensity
********************************************************************************

* With controls (+ store p-values + store samples without singletons)
eststo: qui reghdfe voted19 t_f t_w  $othervars if inrange(votepropensity,-50,40), absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace)
	estadd local sample "$<$40"
eststo: qui reghdfe voted19 t_f t_w  $othervars if inrange(votepropensity,40,60), absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace)
	estadd local sample "40--60"
eststo: qui reghdfe voted19 t_f t_w  $othervars if inrange(votepropensity,60,150), absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace)
	estadd local sample "$>$60"
eststo: qui reghdfe voted19 t_f_l t_f_n t_w_l t_w_n  $othervars if inrange(votepropensity,-50,40), absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace) 
	estadd local sample "$<$40"
eststo: qui reghdfe voted19 t_f_l t_f_n t_w_l t_w_n  $othervars if inrange(votepropensity,40,60), absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace) 
	estadd local sample "40--60"
eststo: qui reghdfe voted19 t_f_l t_f_n t_w_l t_w_n  $othervars if inrange(votepropensity,60,150), absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace) 
	estadd local sample "$>$60"
esttab, $statatab keep(t_f t_w t_f_l t_f_n t_w_n t_w_l) stats(N sample, fmt(%11.0fc) layout("\mc{@}" "\mc{@}") label("Observations" "Vote propensity"))
esttab using "$out/tex/spillover_timing.tex", $latextab keep(t_f t_w t_f_l t_f_n t_w_n t_w_l) stats(N sample, fmt(%11.0fc) layout("\mc{@}" "\mc{@}") label("Observations" "Vote propensity"))
eststo clear


********************************************************************************
// Rolling regressions
********************************************************************************

* Set bandwidth to 15
gen vp = .
gen weight = .
gen b = .
gen b_f = .
gen b_w = .
gen b_f_l = .
gen b_w_l = .
gen b_f_n = .
gen b_w_n = .
gen se = .
gen se_f = .
gen se_w = .
gen se_f_l = .
gen se_w_l = .
gen se_f_n = .
gen se_w_n = .

local r 0
forvalues i = 0(1)100 {
	local r = `r' + 1
	replace vp = `i' if _n == `r'
	replace weight = max(0,15-abs(votepropensity-`i'))
	
	qui reghdfe voted19 treat $othervars [aw=weight] if weight > 0, absorb($fixed_absorb $fixed_transform) cluster(family)
	replace b = _b[treat] if _n == `r'
	replace se = _se[treat] if _n == `r'
	
	* Spillover
	qui reghdfe voted19 t_f t_w  $othervars [aw=weight] if weight > 0, absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace) 
	replace b_f = _b[t_f] if _n == `r'
	replace se_f = _se[t_f] if _n == `r'
	replace b_w = _b[t_w] if _n == `r'
	replace se_w = _se[t_w] if _n == `r'
	
	* Spillover at lunch
	qui reghdfe voted19 t_f_l t_w_l  $othervars [aw=weight] if weight > 0, absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace) 
	replace b_f_l = _b[t_f_l] if _n == `r'
	replace se_f_l = _se[t_f_l] if _n == `r'
	replace b_w_l = _b[t_w_l] if _n == `r'
	replace se_w_l = _se[t_w_l] if _n == `r'

	* Spillover at night
	qui reghdfe voted19 t_f_n t_w_n  $othervars [aw=weight] if weight > 0, absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace) 
	replace b_f_n = _b[t_f_n] if _n == `r'
	replace se_f_n = _se[t_f_n] if _n == `r'
	replace b_w_n = _b[t_w_n] if _n == `r'
	replace se_w_n = _se[t_w_n] if _n == `r'
	
}

* Interpolate standard errors
replace se = . if se == 0
ipolate se vp if vp != ., gen(se_i)

* Calculate confidence interval
gen ll = b - 1.96*se_i
gen ul = b + 1.96*se_i
gen ll_f = b_f - 1.96*se_f
gen ul_f = b_f + 1.96*se_f
gen ll_w = b_w - 1.96*se_w
gen ul_w = b_w + 1.96*se_w
foreach i in f w {
	foreach t in l n {
		gen ll_`i'_`t' = b_`i'_`t' - 1.96*se_`i'_`t'
		gen ul_`i'_`t' = b_`i'_`t' + 1.96*se_`i'_`t'
	}
}

* Direct
tw (hist votepropensity if inrange(votepropensity,0,100) & treat != ., color(gs14) yaxis(2)) ///
(line b ll ul vp, yline(0,lwidth(medthin)) scheme(s1manual) graphregion(fcolor(none) ifcolor(none)) ///
	lpattern(solid dash dash) lcolor(gs3 gs3 gs3)) ///
(function y = 0, range(0 100) lpattern(solid) lwidth(medthin) ///
	xtitle("Vote propensity") ytitle("Estimated effect on turnout (p.p.)") ///
	legend(off) yaxis(1) yla(-1.5(0.5)1.5)) 
graph export "$out/img/Rolling regressions direct.pdf", as(pdf) replace

* Family
tw (hist votepropensity if inrange(votepropensity,0,100), color(gs14) yaxis(2)) ///
(line b_f ll_f ul_f vp, yline(0, lwidth(medthin)) scheme(s1manual) graphregion(fcolor(none) ifcolor(none)) ///
	lpattern(solid dash dash) lcolor(gs3 gs3 gs3)) ///
(function y = 0, range(0 100) lpattern(solid) lwidth(medthin) ///
	xtitle("Vote propensity") ytitle("Estimated effect on turnout (p.p.)") ///
	legend(off) yaxis(1) yla(-1.5(0.5)1.5)) 
graph export "$out/img/Rolling regressions family.pdf", as(pdf) replace

* Work
tw (hist votepropensity if inrange(votepropensity,0,100), color(gs14) yaxis(2)) ///
(line b_w ll_w ul_w vp, yline(0, lwidth(medthin)) scheme(s1manual) graphregion(fcolor(none) ifcolor(none)) ///
	lpattern(solid dash dash) lcolor(gs3 gs3 gs3)) ///
(function y = 0, range(0 100) lpattern(solid) lwidth(medthin) ///
	xtitle("Vote propensity") ytitle("Estimated effect on turnout (p.p.)") ///
	legend(off) yaxis(1) yla(-1.5(0.5)1.5)) 
graph export "$out/img/Rolling regressions workplace.pdf", as(pdf) replace

* Family at lunch
tw (hist votepropensity if inrange(votepropensity,0,100), color(gs14) yaxis(2)) ///
(line b_f_l ll_f_l ul_f_l vp, yline(0, lwidth(medthin)) scheme(s1manual) graphregion(fcolor(none) ifcolor(none)) ///
	lpattern(solid dash dash) lcolor(gs3 gs3 gs3)) ///
(function y = 0, range(0 100) lpattern(solid) lwidth(medthin) ///
	xtitle("Vote propensity") ytitle("Estimated effect on turnout (p.p.)") ///
	legend(off) yaxis(1) yla(-1.5(0.5)1.5)) 
graph export "$out/img/Rolling regressions family lunch.pdf", as(pdf) replace

* Work at lunch
tw (hist votepropensity if inrange(votepropensity,0,100), color(gs14) yaxis(2)) ///
(line b_w_l ll_w_l ul_w_l vp, yline(0, lwidth(medthin)) scheme(s1manual) graphregion(fcolor(none) ifcolor(none)) ///
	lpattern(solid dash dash) lcolor(gs3 gs3 gs3)) ///
(function y = 0, range(0 100) lpattern(solid) lwidth(medthin) ///
	xtitle("Vote propensity") ytitle("Estimated effect on turnout (p.p.)") ///
	legend(off) yaxis(1) yla(-1.5(0.5)1.5)) 
graph export "$out/img/Rolling regressions workplace lunch.pdf", as(pdf) replace

* Family at night
tw (hist votepropensity if inrange(votepropensity,0,100), color(gs14) yaxis(2)) ///
(line b_f_n ll_f_n ul_f_n vp, yline(0, lwidth(medthin)) scheme(s1manual) graphregion(fcolor(none) ifcolor(none)) ///
	lpattern(solid dash dash) lcolor(gs3 gs3 gs3)) ///
(function y = 0, range(0 100) lpattern(solid) lwidth(medthin) ///
	xtitle("Vote propensity") ytitle("Estimated effect on turnout (p.p.)") ///
	legend(off) yaxis(1) yla(-1.5(0.5)1.5)) 
graph export "$out/img/Rolling regressions family evening.pdf", as(pdf) replace

* Work at night
tw (hist votepropensity if inrange(votepropensity,0,100), color(gs14) yaxis(2)) ///
(line b_w_n ll_w_n ul_w_n vp, yline(0, lwidth(medthin)) scheme(s1manual) graphregion(fcolor(none) ifcolor(none)) ///
	lpattern(solid dash dash) lcolor(gs3 gs3 gs3)) ///
(function y = 0, range(0 100) lpattern(solid) lwidth(medthin) ///
	xtitle("Vote propensity") ytitle("Estimated effect on turnout (p.p.)") ///
	legend(off) yaxis(1) yla(-1.5(0.5)1.5)) 
graph export "$out/img/Rolling regressions workplace evening.pdf", as(pdf) replace

* Clean
drop vp weight b b_f* se se_f* se_i ll_f* ul_f* ll ul se_i


*** Bivariate
gen vp = .
gen weight = .
gen b = .
gen b_f = .
gen se = .
gen se_f = .
local r 0
forvalues i = 0(1)100 {
	local r = `r' + 1
	replace vp = `i' if _n == `r'
	replace weight = max(0,15-abs(votepropensity-`i'))
	qui reg voted19 treat [aw=weight] if weight > 0, cluster(family)
	replace b = _b[treat] if _n == `r'
	replace se = _se[treat] if _n == `r'
	qui reghdfe voted19 t_f t_w [aw=weight] if weight > 0, absorb(sf_f sf_w) cluster(family workplace) 
	replace b_f = _b[t_f] if _n == `r'
	replace se_f = _se[t_f] if _n == `r'
}

* Interpolate standard errors
replace se = . if se == 0
ipolate se vp if vp != ., gen(se_i)

gen ll = b - 1.96*se_i
gen ul = b + 1.96*se_i
gen ll_f = b_f - 1.96*se_f
gen ul_f = b_f + 1.96*se_f

* Direct
tw (hist votepropensity if inrange(votepropensity,0,100) & treat != ., color(gs14) yaxis(2)) ///
(line b ll ul vp, yline(0) scheme(s1manual) graphregion(fcolor(none) ifcolor(none)) ///
	lpattern(solid dash dash) lcolor(gs3 gs3 gs3) ///
	xtitle("Vote propensity") ytitle("Estimated effect on turnout (p.p.)") ///
	legend(off) yaxis(1) yla(-1.5(0.5)1.5)) 
graph export "$out/img/Rolling regressions direct bi.pdf", as(pdf) replace

* Only family
tw (hist votepropensity if inrange(votepropensity,0,100), color(gs14) yaxis(2)) ///
(line b_f ll_f ul_f vp, yline(0) scheme(s1manual) graphregion(fcolor(none) ifcolor(none)) ///
	lpattern(solid dash dash) lcolor(gs3 gs3 gs3) ///
	xtitle("Vote propensity") ytitle("Estimated effect on turnout (p.p.)") ///
	legend(off) yaxis(1) yla(-1.5(0.5)1.5)) 
graph export "$out/img/Rolling regressions family bi.pdf", as(pdf) replace

* Clean
drop vp weight b b_f b_w se se_f se_w ll_f ul_f ll_w ul_w ll ul se_i


********************************************************************************
// Rolling regressions firm size
********************************************************************************

bys workplace: egen wpsize = count(LopNr)

* Set bandwidth to 15
gen size = .
gen weight = .
gen b = .
gen b_f = .
gen b_w = .
gen se = .
gen se_f = .
gen se_w = .


local r 0
forvalues i = 0(1)50 {
	local r = `r' + 1
	replace size = `i' if _n == `r'
	replace weight = max(0,15-abs(wpsize-`i')) // Mindre än 15?
	
	qui reghdfe voted19 treat $othervars [aw=weight] if weight > 0, absorb($fixed_absorb $fixed_transform) cluster(family)
	replace b = _b[treat] if _n == `r'
	replace se = _se[treat] if _n == `r'
	
	* Spillover
	qui reghdfe voted19 t_f t_w  $othervars [aw=weight] if weight > 0, absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace) 
	replace b_f = _b[t_f] if _n == `r'
	replace se_f = _se[t_f] if _n == `r'
	replace b_w = _b[t_w] if _n == `r'
	replace se_w = _se[t_w] if _n == `r'
}

* Interpolate standard errors
replace se = . if se == 0
ipolate se size if size != ., gen(se_i)

* Calculate confidence interval
gen ll = b - 1.96*se_i
gen ul = b + 1.96*se_i
gen ll_w = b_w - 1.96*se_w
gen ul_w = b_w + 1.96*se_w

* Direct
tw (hist wpsize if inrange(wpsize,0,50) & treat != ., discrete color(gs14)  yla(0(0.05)0.15,axis(2)) yaxis(2)) ///
(line b ll ul size, yline(0,lwidth(medthin)) scheme(s1manual) graphregion(fcolor(none) ifcolor(none)) ///
	lpattern(solid dash dash) lcolor(gs3 gs3 gs3)) ///
(function y = 0, range(0 50) lpattern(solid) lwidth(medthin) ///
	xtitle("Number of colleagues") ytitle("Estimated effect on turnout (p.p.)") ///
	legend(off) yaxis(1) yla(-1.5(0.5)1.5)) 
graph export "$out/img/Rolling regressions size direct.pdf", as(pdf) replace

* Work
tw (hist wpsize if inrange(wpsize,0,50), discrete color(gs14) yla(0(0.05)0.15,axis(2)) yaxis(2)) ///
(line b_w ll_w ul_w size, yline(0, lwidth(medthin)) scheme(s1manual) graphregion(fcolor(none) ifcolor(none)) ///
	lpattern(solid dash dash) lcolor(gs3 gs3 gs3)) ///
(function y = 0, range(0 50) lpattern(solid) lwidth(medthin) ///
	xtitle("Number of colleagues") ytitle("Estimated effect on turnout (p.p.)") ///
	legend(off) yaxis(1) yla(-1.5(0.5)1.5)) 
graph export "$out/img/Rolling regressions size workplace.pdf", as(pdf) replace

* Clean
drop size wpsize weight b b_f b_w se se_f se_w ll ul se_i ll_w* ul_w*


********************************************************************************
// Rolling regressions firm vote propensity
********************************************************************************

* Old measure
bys workplace: egen wpvp_old = mean(votepropensity)
replace wpvp_old = . if workplace == 0

* New measure without index person
bys workplace: egen wpsum = total(votepropensity)
bys workplace: egen wpcount = count(votepropensity)
gen wpvp = (wpsum-votepropensity)/(wpcount-1)
replace wpvp = . if workplace == 0
drop wpsum wpcount

* Set bandwidth to 15
gen vp = .
gen weight = .
gen b = .
gen b_f = .
gen b_w = .
gen se = .
gen se_f = .
gen se_w = .

local r 0
forvalues i = 0(1)100 {
	local r = `r' + 1
	replace vp = `i' if _n == `r'
	replace weight = max(0,15-abs(wpvp-`i')) // Mindre än 15?
	
	qui reghdfe voted19 treat $othervars [aw=weight] if weight > 0, absorb($fixed_absorb $fixed_transform) cluster(family)
	replace b = _b[treat] if _n == `r'
	replace se = _se[treat] if _n == `r'
	
	* Spillover
	qui reghdfe voted19 t_f t_w  $othervars [aw=weight] if weight > 0, absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace) 
	replace b_f = _b[t_f] if _n == `r'
	replace se_f = _se[t_f] if _n == `r'
	replace b_w = _b[t_w] if _n == `r'
	replace se_w = _se[t_w] if _n == `r'

}

* Interpolate standard errors
replace se = . if se == 0
ipolate se wpvp if wpvp != ., gen(se_i)

* Calculate confidence interval
gen ll = b - 1.96*se
gen ul = b + 1.96*se
gen ll_w = b_w - 1.96*se_w
gen ul_w = b_w + 1.96*se_w

* Direct
replace ll = . if ll < -2
replace ul = . if ul > 2
tw (hist wpvp if inrange(wpvp,0,100) & treat != ., color(gs14) yla(0(0.01)0.03,axis(2)) yaxis(2)) ///
(line b ll ul vp, yline(0,lwidth(medthin)) scheme(s1manual) graphregion(fcolor(none) ifcolor(none)) ///
	lpattern(solid dash dash) lcolor(gs3 gs3 gs3)) ///
(function y = 0, range(0 100) lpattern(solid) lwidth(medthin) ///
	xtitle("Vote propensity at workplace") ytitle("Estimated effect on turnout (p.p.)") ///
	legend(off) yaxis(1) yla(-2(1)2)) 
graph export "$out/img/Rolling regressions wpvp direct.pdf", as(pdf) replace

* Work
replace ll_w = . if ll_w < -2
replace ul_w = . if ul_w > 2
tw (hist wpvp if inrange(wpvp,0,100), color(gs14) yla(0(0.01)0.03,axis(2)) yaxis(2)) ///
(line b_w ll_w ul_w vp, yline(0, lwidth(medthin)) scheme(s1manual) graphregion(fcolor(none) ifcolor(none)) ///
	lpattern(solid dash dash) lcolor(gs3 gs3 gs3)) ///
(function y = 0, range(0 100) lpattern(solid) lwidth(medthin) ///
	xtitle("Vote propensity at workplace") ytitle("Estimated effect on turnout (p.p.)") ///
	legend(off) yaxis(1) yla(-2(1)2)) 
graph export "$out/img/Rolling regressions wpvp workplace.pdf", as(pdf) replace

* Clean
drop vp wpvp weight b b_f b_w se se_f se_w ll ul se_i ll_w* ul_w*


********************************************************************************
// Balance tests
********************************************************************************

* F-test of main effect but on treat instead
reghdfe treat $othervars, absorb($fixed_absorb $fixed_transform) cluster(family)

* Variables to include in table
local balancevars voted09 voted09_mor voted09_far voted10 voted10_mor voted10_far voted18 voted18_mor voted18_far district_turnout female incdecile familysize yearseducation birthyear

* Classic balance table
preserve
file open table using "$out/tex/balance.tex", write replace
file write table "\begin{tabular}{l*{4}{D{.}{.}{2}}}" _n
file write table "\toprule" _n
file write table "Variable&\multicolumn{1}{c}{Control}&\multicolumn{1}{c}{Treat}&\multicolumn{1}{c}{Dif}&\multicolumn{1}{c}{p}\\" _n
file write table "\midrule" _n
foreach v in `balancevars' {
	if strpos("`v'","voted")>0 | "`v'" == "district_turnout" replace `v' = `v' * 100
	local label: variable label `v'
	su `v' if voted19 != . & treat == 0
	local mean0 = r(mean)
	su `v' if voted19 != . & treat == 1
	local mean1 = r(mean)
	local dif  = `mean1' - `mean0'
	reg `v' treat if voted19 != .
	local t = _b[treat]/_se[treat]
	local p =2*ttail(e(df_r),abs(`t'))
	local mean0 : di %7.2fc `mean0'
	local mean1 : di %7.2fc `mean1'
	local dif : di %7.2fc `dif'
	local p : di %5.2fc `p'
	file write table "`label'&`mean0'&`mean1'&`dif'&`p'\\" _n
}
file write table "\bottomrule" _n
file write table "\end{tabular}" _n
file close table
restore


********************************************************************************
// Main results
********************************************************************************

* Without controls	
eststo: qui reg voted19 treat, cluster(family)
eststo: qui reghdfe voted19 t_f t_w, absorb(sf_f sf_w) cluster(family workplace) 
eststo: qui reghdfe voted19 t_f t_f_f t_f_s t_w t_w_f t_w_s, absorb(sf_f sf_f_f sf_f_s sf_w sf_w_f sf_w_s) cluster(family workplace) 
eststo: qui reghdfe voted19 t_f t_f_n t_w t_w_l, absorb(sf_f sf_w) cluster(family workplace) 
esttab, $statatab
esttab using "$out/tex/main_bivariate.tex", $latextab $stats
eststo clear

* With controls (+ store p-values + store samples without singletons)
capture drop p
gen h = ""
gen p = .
eststo: qui reghdfe voted19 treat $othervars, absorb($fixed_absorb $fixed_transform) cluster(family)
	gen sample_h1 = 1 if e(sample)
	replace h = "h1" if _n == 1
	replace p = min(1,ttail(e(df_r),_b[treat]/_se[treat])) if _n == 1
eststo: qui reghdfe voted19 t_f t_w  $othervars, absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace) 
	gen sample_h2 = 1 if e(sample)
	replace h = "h2a" if _n == 2
	replace p = min(1,ttail(e(df_r),_b[t_f]/_se[t_f])) if _n == 2
	replace h = "h2b" if _n == 3
	replace p = min(1,ttail(e(df_r),_b[t_w]/_se[t_w])) if _n == 3
eststo: qui reghdfe voted19 t_f t_f_f t_f_s t_w t_w_f t_w_s $othervars, absorb($fixed_absorb $fixed_transform sf_f sf_f_f sf_f_s sf_w sf_w_f sf_w_s) cluster(family workplace) 
	gen sample_h3 = 1 if e(sample)
	replace h = "h3a" if _n == 4
	replace p = min(1,ttail(e(df_r),_b[t_f_f]/_se[t_f_f])) if _n == 4
	replace h = "h3b" if _n == 5
	replace p = min(1,ttail(e(df_r),_b[t_w_s]/_se[t_w_s])) if _n == 5
eststo: qui reghdfe voted19 t_f t_f_n t_w t_w_l  $othervars, absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family workplace) 
	gen sample_h4 = 1 if e(sample)
	replace h = "h4a" if _n == 6
	replace p = min(1,ttail(e(df_r),_b[t_f]/_se[t_f])) if _n == 6
	replace h = "h4b" if _n == 7
	replace p = min(1,ttail(e(df_r),_b[t_w]/_se[t_w])) if _n == 7
esttab, $statatab $mainkeep
esttab using "$out/tex/main.tex", $latextab $mainkeep $stats
eststo clear

* Same as above but only clustered on family
eststo: qui reghdfe voted19 treat $othervars, absorb($fixed_absorb $fixed_transform) cluster(family)
eststo: qui reghdfe voted19 t_f t_w  $othervars, absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family) 
eststo: qui reghdfe voted19 t_f t_f_f t_f_s t_w t_w_f t_w_s $othervars, absorb($fixed_absorb $fixed_transform sf_f sf_f_f sf_f_s sf_w sf_w_f sf_w_s) cluster(family) 
eststo: qui reghdfe voted19 t_f t_f_n t_w t_w_l  $othervars, absorb($fixed_absorb $fixed_transform sf_f sf_w) cluster(family) 
esttab, $statatab $mainkeep
esttab using "$out/tex/main_oldse.tex", $latextab $mainkeep $stats
eststo clear


********************************************************************************
// Heterogenous effects
********************************************************************************

* Destring education
destring Sun2000niva_Old, replace force 

* Write program
capture program drop hetero
program define hetero
syntax anything, label(string) [sample(string) title(string) emptyrowafter emptyrowbefore onlybi onlylunch onlynight]

	* Modify options
	assert "`onlylunch'" == "" | "`onlynight'" == ""
	if "`sample'" == "" local sample 0==0

	* Run bivariate regressions and store coefficients
	if "`onlylunch'" == "" & "`onlynight'" == "" {
		qui reg voted19 treat if `sample', cluster(family)
			local b_bi = _b[treat]
			local se_bi = _se[treat]
	}	
	if "`onlylunch'" != "" {
		qui reg voted19 treat_lunch treat_night if `sample', cluster(family)
			local b_bi = _b[treat_lunch]
			local se_bi = _se[treat_lunch]
	}	
	if "`onlynight'"  != "" {
		qui reg voted19 treat_lunch treat_night if `sample', cluster(family)
			local b_bi = _b[treat_night]
			local se_bi = _se[treat_night]		
	}	
	
	* Store other regression coefficients
	local n = e(N)
	local baseline = _b[_cons]
	
	* Run regressions with covariates and store coefficients
	if "`onlylunch'" == "" & "`onlynight'" == "" & "`onlybi'" == "" {
		qui reghdfe voted19 treat $othervars if `sample', absorb($fixed_absorb $fixed_transform) cluster(family)
			local b_cov = _b[treat]
			local se_cov = _se[treat]
	}	
	if "`onlylunch'" != "" & "`onlybi'" == "" {
		qui reghdfe voted19 treat_lunch treat_night $othervars if `sample', absorb($fixed_absorb $fixed_transform) cluster(family)
			local b_cov = _b[treat_lunch]
			local se_cov = _se[treat_lunch]
		}	
	if "`onlynight'"  != "" & "`onlybi'" == "" {
		qui reghdfe voted19 treat_lunch treat_night $othervars if `sample', absorb($fixed_absorb $fixed_transform) cluster(family)
			local b_cov = _b[treat_night]
			local se_cov = _se[treat_night]		
	}
	
	* Format numbers
	local n : di %9.0fc `n'
	foreach v in baseline b_bi se_bi b_cov se_cov {
		local `v' : di %5.2f ``v''
	}
	
	if "`onlybi'" != "" {
		local b_cov \mc{--}
		local se_cov \mc{--}
	}
	
	* Write table rows
	if "`emptyrowbefore'" != "" file write table "&&&&&&\\" _n
	if "`title'" != "" file write table "\multicolumn{7}{l}{\emph{`title'}}\\" _n 
	file write table "`label'&`n'&`baseline'&`b_bi'&`se_bi'&`b_cov'&`se_cov'\\" _n
	if "`emptyrowafter'" != "" file write table "&&&&&&\\" _n

end

* Use program
capture file close table
file open table using "$out/tex/hetero.tex", write replace
file write table "\begin{tabular}{lD{.}{.}{0}*{5}{D{.}{.}{2}}}" _n
file write table "\toprule" _n
file write table "&&&\multicolumn{2}{c}{Bivariate}&\multicolumn{2}{c}{Covariates}\\" _n
file write table "\cmidrule(lr){4-5}" _n
file write table "\cmidrule(lr){6-7}" _n
file write table "\mc{Subgroup}&\mc{Obs}&\mc{Turnout}&\mc{Effect}&\mc{SE}&\mc{Effect}&\mc{SE}\\" _n
file write table "\midrule" _n
hetero table, label(Full sample)
hetero table, sample(votepropensity<40) label(Below 40 percent) title(Vote propensity) emptyrowbefore
hetero table, sample(inrange(votepropensity,40,60)) label(40--60 percent)
hetero table, sample(votepropensity>60) label(Above 60 percent)
hetero table, sample(district_turnout<0.40) label(Below 40 percent) title(District turnout) emptyrowbefore
hetero table, sample(inrange(district_turnout,0.40,0.60)) label(40--60 percent)
hetero table, sample(district_turnout>0.60) label(Above 60 percent) 
hetero table, sample(votedcount==0) label(None) title(Voting history) emptyrowbefore
hetero table, sample(votedcount==1) label(One election) 
hetero table, sample(votedcount==2) label(Two elections)
hetero table, sample(votedcount==3) label(All three elections)
hetero table, sample(female==1) label(Female) title(Sex) emptyrowbefore
hetero table, sample(female==0) label(Male)
hetero table, sample(age<30) label(18--30 years old) title(Age) emptyrowbefore
hetero table, sample(inrange(age,30,50)) label(30--50 years old)
hetero table, sample(age>50) label(Older than 50)
hetero table, sample(inlist(UtlSvBakG,11,12)) label(Foreign background) title(Immigrant background) emptyrowbefore
hetero table, sample(inlist(UtlSvBakG,21,22,23)) label(Swedish background)
hetero table, sample(inrange(Sun2000niva_Old,1,3)) label(SUN 1--3) title(Education) emptyrowbefore
hetero table, sample(inrange(Sun2000niva_Old,4,6)) label(SUN 4--6) 
hetero table, sample(Sun2000niva_Old==7) label(SUN 7) onlybi
file write table "\bottomrule" _n
file write table "\end{tabular}" _n
file close table

* Labels below
label var treat_lunch "Treated directly at lunch"
label var treat_night "Treated directly in evening"
gen treat_dif = treat_night
label var treat_dif "Difference"

* Direct effects lunch vs night
eststo: qui reghdfe voted19 treat $othervars, absorb($fixed_absorb $fixed_transform) cluster(family)
eststo: qui reghdfe voted19 treat_lunch treat_night $othervars, absorb($fixed_absorb $fixed_transform) cluster(family)
lincom treat_night - treat_lunch
estadd scalar dif_b = r(estimate)
estadd scalar dif_se = r(se)
estadd scalar dif_p = r(p)
esttab, $statatab keep(treat*) stats(dif_b dif_se dif_p N, fmt(%6.3f %6.3f %6.3f %11.0fc) layout("@" "@" "@" "\mc{@}") label("Difference" "Standard error" "p-value" "Observations"))
esttab using "$out/tex/direct_lunchnight.tex", $latextab keep(treat*) stats(dif_b dif_se dif_p N, fmt(%6.3f %6.3f %6.3f %11.0fc) layout("@" "@" "@" "\mc{@}") label("Difference" "Standard error" "p-value" "Observations")) 
eststo clear




********************************************************************************
// Other thresholds
********************************************************************************

* Workplaces with different 10-thresholds
gen b = .
gen se = .
forvalues i = 0(5)20 {
	local r = `i'/5 + 1
	qui reghdfe voted19 t_f t_w_10to`i' $othervars, absorb($fixed_absorb $fixed_transform sf_f sf_w_10to`i') cluster(family w_10to`i') 
	replace b = _b[t_w_10to`i'] if _n == `r'
	replace se = _se[t_w_10to`i'] if _n == `r'
}

gen ul = b + 1.96*se
gen ll = b - 1.96*se
gen r = _n if b != .

tw (scatter r b) (rspike ll ul r, horizontal), xline(0) scheme(lean1) legend(off)  ///
ylabel(1 "0" 2 "5" 3 "10" 4 "15" 5 "20")  graphregion(margin(0 1 1 1)) ysc(outergap(-0)) ytitle("Threshold") xtitle("Estimated spillover") ///
xla(-0.1(0.1)0.1) xsc(range(-0.15 0.15))
graph export "$out/img/Other thresholds 10.pdf", as(pdf) replace
drop b se ul ll r

* Workplaces with different 50-thresholds
gen b = .
gen se = .
forvalues i = 25(25)100 {
	local r = `i'/25
	qui reghdfe voted19 t_f t_w_50to`i' $othervars, absorb($fixed_absorb $fixed_transform sf_f sf_w_50to`i') cluster(family w_50to`i') 
	replace b = _b[t_w_50to`i'] if _n == `r'
	replace se = _se[t_w_50to`i'] if _n == `r'
}

* Slow model
qui reghdfe voted19 t_f t_w_none  $othervars, absorb($fixed_absorb $fixed_transform sf_f sf_w_none) cluster(family w_none) 

* Export
replace b = _b[t_w_none] if _n == 5
replace se = _se[t_w_none] if _n == 5
gen ul = b + 1.96*se
gen ll = b - 1.96*se
gen r = _n if b != .

tw (scatter r b) (rspike ll ul r, horizontal), xline(0) scheme(lean1) legend(off) ///
ylabel(1 "25" 2 "50" 3 "75" 4 "100" 5 "None")  graphregion(margin(0 1 1 1)) ysc(outergap(-0)) ytitle("Threshold") xtitle("Estimated spillover") ///
xla(-0.1(0.1)0.1) xsc(range(-0.15 0.15))
graph export "$out/img/Other thresholds 50.pdf", as(pdf) replace
drop b se ul ll r


********************************************************************************
// Graph from 2009 election
********************************************************************************

* Open dataset
use "E:\ProjData\GOTV\Dataset 2009.dta", replace

* Variable macros
global fixed_absorb_2009 FodAr 
global fixed_transform_2009 SunInr UtlSvBakg DecInk
global othervars_2009 MorRost94_r MorRost94_f FarRost94_r FarRost94_f Rost94_r Rost94_f rost_vd UtbAr Kon FamStor
   
capture drop votepropensity
reghdfe Rost09 $othervars_2009, absorb($fixed_absorb_2009 $fixed_transform_2009)
predict votepropensity
    
reghdfe Rost09 i.Part $othervars_2009, absorb($fixed_absorb_2009 $fixed_transform_2009)
	
gen vp = .
gen weight = .
gen b = .
gen b1 = .
gen b2 = .
gen se1 = .
gen se2 = .
gen ul = .
gen ll = .
gen se = .
  
keep if inrange(FodAr, 1934, 1991)
  
local r 0
forvalues i = 0(1)100 {
	local r = `r' + 1
	qui: replace vp = `i' if _n == `r'
	qui: replace weight = max(0,15-abs(votepropensity-`i'))
	
	qui reghdfe Rost09 i.PartTypRost $othervars_2009 [aw=weight] if weight > 0, absorb($fixed_absorb_2009 $fixed_transform_2009) 
	lincom _b[2.PartTypRost]-_b[1.PartTypRost]
	qui replace ll = r(lb) if _n == `r'
	qui replace ul = r(ub) if _n == `r'
	
	qui replace b1 = _b[1.PartTypRost] if _n == `r'
	qui replace b2 = _b[2.PartTypRost] if _n == `r'
	qui replace b = r(estimate) if _n == `r'
	qui replace se1 = _se[1.PartTypRost] if _n == `r'
	qui replace se2 = _se[2.PartTypRost] if _n == `r'
	qui replace se = r(se) if _n == `r'
	di "Antal rundor:"
	di `r'
}
	
sort vp
tw 	(hist votepropensity if inrange(votepropensity,0,100) & PartTypRost != ., color(gs14) yaxis(2)) ///
	(line b ll ul vp, yline(0) scheme(s1manual) graphregion(fcolor(none) ifcolor(none)) ///
	lpattern(solid dash dash) lcolor(gs3 gs3 gs3) ///
	xtitle("Vote propensity") ytitle("Vote Difference (p.p.)") ///
	legend(off) yaxis(1) yla(0(1)10))

graph export "$out/img/EP2009_plot.pdf", as(pdf) replace


********************************************************************************
// The end
********************************************************************************

* Close log
log close









